package com.hunliji.monitor.service.api.model.bo;

import com.google.common.collect.Lists;
import com.hunliji.common.utils.DateUtil;
import com.hunliji.monitor.service.api.model.param.AlertNotifyParam;
import java.util.Date;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

/**
 * File：FeishuPostMessageBO.java<br> Title: <br> Description: <br>
 *
 * @author lejin Date: 2022/10/11
 */
@NoArgsConstructor
@Data
@Builder
@AllArgsConstructor
public class FeishuPostMessageBO {

    private FeishuPostMessageZhCnBO zh_cn;

    public static FeishuPostMessageBO getDefaultFeishuPostMessage(
        AlertNotifyParam alertNotifyParam, List<UserInfoBO> atUsers){
        FeishuPostMessageBO feishuPostMessageBO = new FeishuPostMessageBO();
        FeishuPostMessageZhCnBO zhCnBean = new FeishuPostMessageZhCnBO();
        zhCnBean.setTitle("指标告警异常通知");
        FeishuPostMessageContentBO contentBean1 = FeishuPostMessageContentBO.builder().tag("text").text("告警规则名称:" + alertNotifyParam.getAlertName()).build();
        List<FeishuPostMessageContentBO> contentBeans1 = Lists.newArrayList(contentBean1);
        FeishuPostMessageContentBO contentBean2 = FeishuPostMessageContentBO.builder().tag("text").text("告警描述:" + alertNotifyParam.getAlertMsg()).build();
        List<FeishuPostMessageContentBO> contentBeans2 = Lists.newArrayList(contentBean2);
        FeishuPostMessageContentBO contentBean3 = FeishuPostMessageContentBO.builder().tag("text").text("告警数量:" + alertNotifyParam.getAlertCount()).build();
        List<FeishuPostMessageContentBO> contentBeans3 = Lists.newArrayList(contentBean3);
        FeishuPostMessageContentBO contentBean4 = FeishuPostMessageContentBO.builder().tag("text").text("触发时间:" + alertNotifyParam.getAlertTime()).build();
        List<FeishuPostMessageContentBO> contentBeans4 = Lists.newArrayList(contentBean4);
        FeishuPostMessageContentBO contentBean51 = FeishuPostMessageContentBO.builder().tag("text").text("告警规则地址:").build();
        FeishuPostMessageContentBO contentBean52 = FeishuPostMessageContentBO.builder().tag("a").text("点我进入").href(alertNotifyParam.getAlertUrl()).build();
        List<FeishuPostMessageContentBO> contentBeans5 = Lists.newArrayList(contentBean51, contentBean52);
        List<FeishuPostMessageContentBO> contentBeans6 = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(atUsers)){
            atUsers.forEach(atUser -> {
                contentBeans6.add(FeishuPostMessageContentBO.builder().tag("at").user_id(atUser.takeFeishuUserId()).user_name(atUser.getName()).build());
            });
        }
        List<List<FeishuPostMessageContentBO>> contents = Lists.newArrayList(contentBeans1, contentBeans2, contentBeans3, contentBeans4, contentBeans5, contentBeans6);
        zhCnBean.setContent(contents);
        feishuPostMessageBO.setZh_cn(zhCnBean);
        return feishuPostMessageBO;
    }

    /**
     * 行动策略消息体
     * @param alertNotifyParam
     * @param atUsers
     * @return
     */
    public static FeishuPostMessageBO getActionStrategyFeishuPostMessage(
            AlertNotifyParam alertNotifyParam, List<UserInfoBO> atUsers){
        FeishuPostMessageBO feishuPostMessageBO = new FeishuPostMessageBO();
        FeishuPostMessageZhCnBO zhCnBean = new FeishuPostMessageZhCnBO();
        zhCnBean.setTitle(alertNotifyParam.getCurrentKey() + "异常通知");
        //FeishuPostMessageContentBO contentBean1 = FeishuPostMessageContentBO.builder().tag("text").text("告警规则名称:" + alertNotifyParam.getAlertName()).build();
        //List<FeishuPostMessageContentBO> contentBeans1 = Lists.newArrayList(contentBean1);
        FeishuPostMessageContentBO contentBean2 = FeishuPostMessageContentBO.builder().tag("text").text("告警描述:" + alertNotifyParam.getAlertMsg()).build();
        List<FeishuPostMessageContentBO> contentBeans2 = Lists.newArrayList(contentBean2);
        //FeishuPostMessageContentBO contentBean3 = FeishuPostMessageContentBO.builder().tag("text").text("告警数量:" + alertNotifyParam.getAlertCount()).build();
        //List<FeishuPostMessageContentBO> contentBeans3 = Lists.newArrayList(contentBean3);
        FeishuPostMessageContentBO contentBean4 = FeishuPostMessageContentBO.builder().tag("text").text("触发时间:" + alertNotifyParam.getAlertTime()).build();
        List<FeishuPostMessageContentBO> contentBeans4 = Lists.newArrayList(contentBean4);
        //FeishuPostMessageContentBO contentBean51 = FeishuPostMessageContentBO.builder().tag("text").text("告警规则地址:").build();
        //FeishuPostMessageContentBO contentBean52 = FeishuPostMessageContentBO.builder().tag("a").text("点我进入").href(alertNotifyParam.getAlertUrl()).build();
        //List<FeishuPostMessageContentBO> contentBeans5 = Lists.newArrayList(contentBean51, contentBean52);
        List<FeishuPostMessageContentBO> contentBeans6 = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(atUsers)){
            atUsers.forEach(atUser -> {
                contentBeans6.add(FeishuPostMessageContentBO.builder().tag("at").user_id(atUser.takeFeishuUserId()).user_name(atUser.getName()).build());
            });
        }
        List<List<FeishuPostMessageContentBO>> contents = Lists.newArrayList(contentBeans2, contentBeans4, contentBeans6);
        zhCnBean.setContent(contents);
        feishuPostMessageBO.setZh_cn(zhCnBean);
        return feishuPostMessageBO;
    }

    /**
     * 发送GitLab飞书评论通知消息
     * @return
     */
    public static FeishuPostMessageBO getDefaultGitLabCommentPostMessage(
        String mergeRequestTitle, String commentContent, String href, String createDate, UserInfoBO userInfoBO){
        FeishuPostMessageBO feishuPostMessageBO = new FeishuPostMessageBO();
        FeishuPostMessageZhCnBO zhCnBean = new FeishuPostMessageZhCnBO();
        zhCnBean.setTitle("GitLab评论通知");
        List<FeishuPostMessageContentBO> contentBeans1 = Lists.newArrayList();
        FeishuPostMessageContentBO contentBean11 = FeishuPostMessageContentBO.builder().tag("text").text("成员 ").build();
        FeishuPostMessageContentBO contentBean12 = FeishuPostMessageContentBO.builder().tag("text").text(userInfoBO.getName()).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean13 = FeishuPostMessageContentBO.builder().tag("text").text(" "+ createDate +" 在您的合并请求【").build();
        FeishuPostMessageContentBO contentBean14 = FeishuPostMessageContentBO.builder().tag("text").text(mergeRequestTitle).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean15 = FeishuPostMessageContentBO.builder().tag("text").text("】上评论了“").build();
        FeishuPostMessageContentBO contentBean16 = FeishuPostMessageContentBO.builder().tag("text").text(commentContent).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean17 = FeishuPostMessageContentBO.builder().tag("text").text("”，请及时关注。").build();
        contentBeans1.add(contentBean11);
        contentBeans1.add(contentBean12);
        contentBeans1.add(contentBean13);
        contentBeans1.add(contentBean14);
        contentBeans1.add(contentBean15);
        contentBeans1.add(contentBean16);
        contentBeans1.add(contentBean17);
        List<FeishuPostMessageContentBO> contentBeans2 = Lists.newArrayList();
        FeishuPostMessageContentBO contentBean21 = FeishuPostMessageContentBO.builder().tag("text").text("地址:").style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean22 = FeishuPostMessageContentBO.builder().tag("text").text(" ").build();
        FeishuPostMessageContentBO contentBean23 = FeishuPostMessageContentBO.builder().tag("a").text("点我进入").href(href).build();
        contentBeans2.add(contentBean21);
        contentBeans2.add(contentBean22);
        contentBeans2.add(contentBean23);
        List<List<FeishuPostMessageContentBO>> contents = Lists.newArrayList(contentBeans1, contentBeans2);
        zhCnBean.setContent(contents);
        feishuPostMessageBO.setZh_cn(zhCnBean);
        return feishuPostMessageBO;
    }

    /**
     * 发送GitLab飞书评论@通知消息
     */
    public static FeishuPostMessageBO getDefaultGitLabCommentAtPostMessage(
        String mergeRequestTitle, String commentContent, String href, String createDate, UserInfoBO userInfoBO, UserInfoBO atUserInfoBO){
        FeishuPostMessageBO feishuPostMessageBO = new FeishuPostMessageBO();
        FeishuPostMessageZhCnBO zhCnBean = new FeishuPostMessageZhCnBO();
        zhCnBean.setTitle("GitLab评论@通知");
        List<FeishuPostMessageContentBO> contentBeans1 = Lists.newArrayList();
        FeishuPostMessageContentBO contentBean11 = FeishuPostMessageContentBO.builder().tag("text").text("成员 ").build();
        FeishuPostMessageContentBO contentBean12 = FeishuPostMessageContentBO.builder().tag("text").text(userInfoBO.getName()).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean13 = FeishuPostMessageContentBO.builder().tag("text").text(" " + createDate + " 在 ").build();
        FeishuPostMessageContentBO contentBean14 = FeishuPostMessageContentBO.builder().tag("text").text(atUserInfoBO.getName()).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean15 = FeishuPostMessageContentBO.builder().tag("text").text(" 的合并请求【").build();
        FeishuPostMessageContentBO contentBean16 = FeishuPostMessageContentBO.builder().tag("text").text(mergeRequestTitle).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean17 = FeishuPostMessageContentBO.builder().tag("text").text("】上@了你，评论内容为“").build();
        FeishuPostMessageContentBO contentBean18 = FeishuPostMessageContentBO.builder().tag("text").text(commentContent).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean19 = FeishuPostMessageContentBO.builder().tag("text").text("”，请及时关注。").build();
        contentBeans1.add(contentBean11);
        contentBeans1.add(contentBean12);
        contentBeans1.add(contentBean13);
        contentBeans1.add(contentBean14);
        contentBeans1.add(contentBean15);
        contentBeans1.add(contentBean16);
        contentBeans1.add(contentBean17);
        contentBeans1.add(contentBean18);
        contentBeans1.add(contentBean19);
        List<FeishuPostMessageContentBO> contentBeans2 = Lists.newArrayList();
        FeishuPostMessageContentBO contentBean21 = FeishuPostMessageContentBO.builder().tag("text").text("地址: ").style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean22 = FeishuPostMessageContentBO.builder().tag("a").text("点我进入").href(href).build();
        contentBeans2.add(contentBean21);
        contentBeans2.add(contentBean22);
        List<List<FeishuPostMessageContentBO>> contents = Lists.newArrayList(contentBeans1, contentBeans2);
        zhCnBean.setContent(contents);
        feishuPostMessageBO.setZh_cn(zhCnBean);
        return feishuPostMessageBO;
    }


    /**
     * 发送GitLab飞书评论通知消息
     * @return
     */
    public static FeishuPostMessageBO getDefaultGitLabCommentPostMessageV2(
        String mergeRequestTitle, String commentContent, String href, String createDate, UserInfoBO userInfoBO){
        FeishuPostMessageBO feishuPostMessageBO = new FeishuPostMessageBO();
        FeishuPostMessageZhCnBO zhCnBean = new FeishuPostMessageZhCnBO();
        zhCnBean.setTitle("Gitlab MR评论通知");
        //content格式：[[{"tag":"text","text":"评论人","style":["bold"]},{"tag":"text","text":" 在合并请求 【****】  中评论了  “**”，请及时关注。","style":[]}],[{"tag":"a","href":"https://git.hunliji.com/merchant/product-develop-center/-/commit/5ef920de59be928d5082850695702d859b917b60","text":"查看详情","style":[]}],[{"tag":"text","text":"评论时间：2023-07-25 03:15:05","style":[]}]]
        List<FeishuPostMessageContentBO> contentBeans1 = Lists.newArrayList();
        FeishuPostMessageContentBO contentBean12 = FeishuPostMessageContentBO.builder().tag("at").user_name(userInfoBO.getName()).user_id(userInfoBO.getFeishuUserId()).build();
        FeishuPostMessageContentBO contentBean13 = FeishuPostMessageContentBO.builder().tag("text").text(" 在合并请求 【").build();
        FeishuPostMessageContentBO contentBean14 = FeishuPostMessageContentBO.builder().tag("text").text(mergeRequestTitle).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean15 = FeishuPostMessageContentBO.builder().tag("text").text("】 中评论了“").build();
        FeishuPostMessageContentBO contentBean16 = FeishuPostMessageContentBO.builder().tag("text").text(commentContent).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean17 = FeishuPostMessageContentBO.builder().tag("text").text("”，请及时关注。").build();
        contentBeans1.add(contentBean12);
        contentBeans1.add(contentBean13);
        contentBeans1.add(contentBean14);
        contentBeans1.add(contentBean15);
        contentBeans1.add(contentBean16);
        contentBeans1.add(contentBean17);
        List<FeishuPostMessageContentBO> contentBeans2 = Lists.newArrayList();
        FeishuPostMessageContentBO contentBean21 = FeishuPostMessageContentBO.builder().tag("a").text("查看详情").href(href).build();
        contentBeans2.add(contentBean21);
        List<List<FeishuPostMessageContentBO>> contents = Lists.newArrayList(contentBeans1, contentBeans2);
        zhCnBean.setContent(contents);
        feishuPostMessageBO.setZh_cn(zhCnBean);
        return feishuPostMessageBO;
    }
    /**
     * 发送GitLab飞书评论@通知消息
     */
    public static FeishuPostMessageBO getDefaultGitLabCommentAtPostMessageV2(
        String mergeRequestTitle, String commentContent, String href, String createDate, UserInfoBO userInfoBO, UserInfoBO atUserInfoBO){
        FeishuPostMessageBO feishuPostMessageBO = new FeishuPostMessageBO();
        FeishuPostMessageZhCnBO zhCnBean = new FeishuPostMessageZhCnBO();
        zhCnBean.setTitle("Gitlab MR评论被 @ 通知");
        //content格式为：{"title":"Gitlab MR评论被 @ 通知","content":[[{"tag":"text","text":"评论人 ","style":["bold"]},{"tag":"text","text":"在 ","style":[]},{"tag":"text","text":"MR创建人 ","style":["bold"]},{"tag":"text","text":"的合并请求 【****】  中@了你，评论内容为：  “****”，请及时关注。","style":[]}],[{"tag":"a","href":"https://git.hunliji.com/merchant/product-develop-center/-/commit/5ef920de59be928d5082850695702d859b917b60","text":"查看详情","style":[]}],[{"tag":"text","text":"评论时间：2023-07-25 03:15:05","style":[]}]]}
        List<FeishuPostMessageContentBO> contentBeans1 = Lists.newArrayList();
        FeishuPostMessageContentBO contentBean12 = FeishuPostMessageContentBO.builder().tag("at").user_name(userInfoBO.getName()).user_id(userInfoBO.getFeishuUserId()).build();
        FeishuPostMessageContentBO contentBean13 = FeishuPostMessageContentBO.builder().tag("text").text(" 在 ").build();
        FeishuPostMessageContentBO contentBean14 = FeishuPostMessageContentBO.builder().tag("text").text("MR创建人 ").build();
        FeishuPostMessageContentBO contentBean141 = FeishuPostMessageContentBO.builder().tag("at").user_name(atUserInfoBO.getName()).user_id(atUserInfoBO.getFeishuUserId()).build();
        FeishuPostMessageContentBO contentBean15 = FeishuPostMessageContentBO.builder().tag("text").text(" 的合并请求 【").build();
        FeishuPostMessageContentBO contentBean16 = FeishuPostMessageContentBO.builder().tag("text").text(mergeRequestTitle).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean17 = FeishuPostMessageContentBO.builder().tag("text").text("】 评论了：“").build();
        FeishuPostMessageContentBO contentBean18 = FeishuPostMessageContentBO.builder().tag("text").text(commentContent).style(Lists.newArrayList("bold")).build();
        FeishuPostMessageContentBO contentBean19 = FeishuPostMessageContentBO.builder().tag("text").text("“，请及时关注。").build();
        contentBeans1.add(contentBean12);
        contentBeans1.add(contentBean13);
        contentBeans1.add(contentBean14);
        contentBeans1.add(contentBean141);
        contentBeans1.add(contentBean15);
        contentBeans1.add(contentBean16);
        contentBeans1.add(contentBean17);
        contentBeans1.add(contentBean18);
        contentBeans1.add(contentBean19);
        List<FeishuPostMessageContentBO> contentBeans2 = Lists.newArrayList();
        FeishuPostMessageContentBO contentBean21 = FeishuPostMessageContentBO.builder().tag("a").text("查看详情").href(href).build();
        contentBeans2.add(contentBean21);
        List<List<FeishuPostMessageContentBO>> contents = Lists.newArrayList(contentBeans1, contentBeans2);
        zhCnBean.setContent(contents);
        feishuPostMessageBO.setZh_cn(zhCnBean);
        return feishuPostMessageBO;
    }
}
